import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/page/health/common/select_date_bar.dart';
import 'package:ty_smart_watch/page/health/controller/common_controller.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_chart_view.dart';
import 'package:ty_smart_watch/page/health/sleep/date_selecet_bar.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_data_view.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_grid_view.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_time_component.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_weakup_component.dart';
import 'package:ty_smart_watch/page/health/sleep/sleep_z_chart_view.dart';

class SleepChartPage extends StatelessWidget {
  const SleepChartPage({super.key});

  @override
  Widget build(BuildContext context) {
    final CommonController controller = Get.put(CommonController());
    controller.getDayBarList();

    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(30.0),
        child: AppBar(
          centerTitle: true,
          title: const Text(
            "睡眠记录",
            style: TextStyle(fontSize: 16),
          ),
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios),
            onPressed: () {
              Get.back();
            },
            iconSize: 16,
          ),
        ),
      ),
      body: Container(
        // margin: EdgeInsets.symmetric(horizontal: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // <2024年11月23日至2024年12月1日>
            DateBar(),
            // 选择< 日 周 月 >
            SelectDateBar(title1: "日", title2: "周", title3: "月", color: Color(0xFF5F76FF)),
            const SizedBox(height: 10),
            // 今日、本周、本月 睡眠报告
            Obx(
              () => Row(
                children: [
                  const SizedBox(width: 10),
                  Text(
                    _getSubTitle(controller.dateTypeIndex.value),
                    style: TextStyle(fontSize: 12),
                  ),
                ],
              )
            ),
            const SizedBox(height: 10),
            //平均睡眠、起床时间
            SleepWeakupComponent(),
            const SizedBox(height: 16),
            //睡眠图表
            ChartView(color: Colors.yellow, maxY: 24),
            //睡眠时间
            SleepTimeComponent(),
            const SizedBox(height: 16),
            SleepGridView(),
            // const SizedBox(height: 10),
            //数据列表信息，仅在“周”“月”中显示。
            Obx(
              () => Visibility(
                visible: controller.dateTypeIndex.value != 0,
                child: Expanded(child: SleepDataView(title: "睡眠")),
              ),
            ),
            //睡眠心率折线图，仅在"日"中显示
            Obx(
              () => Visibility(
                visible: controller.dateTypeIndex.value == 0,
                child: Expanded(child: SleepZChartView()),
              ),
            ),
          ],
        ),
      ),
    );
  }

  String _getSubTitle(int index) {
    String state = "";

    switch (index) {
      case 0:
        state = "今日";
      case 1:
        state = "本周";
      case 2:
        state = "本月";
    }
    return "${state}睡眠报告";
  }
}
